﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Framework;
using CrmContacto.Contracts.Enums;
using CrmContacto.Contracts;
using Web.Framework.Misc;
using CrmContacto.WebApp.Code.InnerClasses;
using Web.CrmContacto.Code;
using Web.CrmContacto.Code.Security;
using CrmContacto.Contracts.Contracts.Search;
using Web.Framework.Search;
using Web.Framework.Translate;

namespace CrmContacto.WebApp.App
{
    public partial class CustomerDelete : BasePage
    {
        private CustomerSearchParam CustomerSearchParamSession
        {
            get
            {
                return (CustomerSearchParam)StoreHelper.GetObject(MiscClass.CustomerSearchParamCacheKey);
            }
            set
            {
                StoreHelper.StoreObject(MiscClass.CustomerSearchParamCacheKey, value);
            }
        }

        private List<LinkButton> actionButtons = new List<LinkButton>();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            LinkButton guardarButton = new LinkButton();
            guardarButton.ID = "BoCommandEliminar";
            guardarButton.Text = LocalizationProvider.Instance["Bo.Command.Eliminar"];
            guardarButton.Click += new EventHandler(lnkEliminar_Click);
            actionButtons.Add(guardarButton);

            LinkButton volverButton = new LinkButton();
            volverButton.ID = "BoCommandVolver";
            volverButton.Text = LocalizationProvider.Instance["Bo.Command.Volver"];
            volverButton.Click += new EventHandler(lnkVolver_Click);
            actionButtons.Add(volverButton);

            base.LoadContextActions(actionButtons);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                HasPermissionToSeeMe(CrmSecurity.CUSTOMER_DELETE);

                if (!IsPostBack)
                {
                    if (CustomerSearchParamSession != null)
                    {
                        BindToGrid();
                    }
                }

                this.pnlResults.CssClass = "displayBlock";
                this.imgExpandResults.ImageUrl = "/Images/icon_blockexpanded.png";
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void BindToGrid()
        {
            if (this.CustomerSearchParamSession.PageSize != 0)
            {
                this.GridCustomer.AllowPaging = true;
                this.GridCustomer.PageSize = this.CustomerSearchParamSession.PageSize;
            }
            else
            {
                this.GridCustomer.AllowPaging = false;
            }

            this.GridCustomer.DataSourceID = "CustomerObjectDs";            
        }       

        public SearchCustomerObjDataSourceProvider CustomerSearchProvider { get; set; }

        protected void CustomerObjectDs_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            try
            {
                GridViewSearchObjectDataSource searchProvider = new GridViewSearchObjectDataSource();
                this.CustomerSearchProvider = new SearchCustomerObjDataSourceProvider();
                this.CustomerSearchProvider.SearchParam = this.CustomerSearchParamSession;
                this.CustomerSearchProvider.WebServiceHelper = this.WebServiceHelper;

                searchProvider.Provider = this.CustomerSearchProvider;
                e.ObjectInstance = searchProvider;
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void GridCustomer_Selected(object sender, ObjectDataSourceStatusEventArgs e)
        {
            try
            {
                if (e.Exception != null)
                {
                    ExceptionManager.Instance.HandleException(e.Exception);
                    e.ExceptionHandled = true;
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void GridCustomer_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {                           
                BindToGrid();
                this.GridCustomer.PageIndex = e.NewPageIndex;    
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void GridCustomer_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Cliente cliente = (Cliente)e.Row.DataItem;

                    if (cliente.TipoDocumentoId.HasValue && cliente.NacionalidadId.HasValue)
                    {
                        foreach (MaestroBase liDocItem in this.GetTipoDocumento(0))
                        {
                            if (liDocItem.Id == cliente.TipoDocumentoId.Value)
                            {
                                e.Row.Cells[4].Text = liDocItem.Nombre;
                                break;
                            }
                        }
                    }
                    else
                    {
                        e.Row.Cells[4].Text = string.Empty;
                    }

                    if (cliente.EstadoEmail.HasValue)
                    {
                        EstadoEmail estadoEmail = (EstadoEmail)cliente.EstadoEmail.Value;
                        e.Row.Cells[6].Text = UIHelper.GetEnumDescription(estadoEmail);
                    }
                    else
                    {
                        e.Row.Cells[6].Text = string.Empty;
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void GridCustomer_DataBound(object sender, EventArgs e)
        {
            try
            {
                if (CustomerSearchProvider != null)
                {
                    ClienteSearchResult searchResult = CustomerSearchProvider.SearchResult;

                    if (searchResult != null && searchResult.TotalDeResultados > 0)
                    {
                        this.pnlResults.CssClass = "displayBlock";
                        this.imgExpandResults.ImageUrl = "/Images/icon_blockexpanded.png";
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkEliminar_Click(object sender, EventArgs e)
        {
            try
            {
                // Borrar con el parámetro de búsqueda y listo
                WebServiceHelper.EliminarContactos(CustomerSearchParamSession);
                BindToGrid();
                this.masterPage.ShowMessage(LocalizationProvider.Instance["Bo.Messages.MensajeSaveExitoso"], WebMessageType.Notification);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkVolver_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlCustomerSearch, true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }            
        }
    }
}